import java.lang.reflect.Array;
import java.util.*;
import java.lang.*;

//类斐波那契数

public class Main {
    public static void main(String[] args) {
        for(int i = 9999999; i >= 0; i--){
            if(judge(i) == true){
                System.out.println(i);
                break;
            }
        }
    }
    public static boolean judge(int n){
        String str = n+"";
        int[] dp = new int[str.length()];
        for(int i = 0; i < str.length(); i++){
            dp[i] = str.charAt(i)-'0';
        }
        int len = str.length();
        int sum = 0;
        for(int i = len; sum < n; i++){
            sum = Arrays.stream(dp).sum();
            dp[i%len] = sum;
        }
        return sum == n;
    }
}